/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "empty.h"


extern Encoder_type encoder1, encoder2;
extern Timer_typeDef timer;

OpenMV_typedef openmv = {0};
PID_Mode current_pid_mode;

float Vofa[4]={0};


volatile uint32_t capturedValue = 0;
volatile uint32_t lastCapturedValue = 0;
float edgeCaptureInterval = 0;


void System_Init(void);


int main(void)
{
    System_Init();
	
	OLED_ShowChar(1,15,'t');

	MOTOR_GiveSpeed(0, 0);
	
	timer.mpu_flag = 1;				// 不读取imu数据
	timer.state = STATE_OPEN_LOOP;	// 开环状态
	current_pid_mode = OPEN_LOOP;	// pid开环模式
	
	delay_ms(1000);
	
    while (1) 
	{
		APP_Task();

//		Vofa[0] = 0;
//		Vofa[1] = fAngle[0];
//		Vofa[2] = fAngle[1];
//		Vofa[3] = fAngle[2];
//		
//		JustFloat_Send(Vofa,4,UART_3_INST);

//		OLED_ShowNum(1,1,timer.ms_count,10);
		
		OLED_ShowNum(1,1,KeyNum[0],2);
		OLED_ShowNum(1,4,KeyNum[1],2);
		OLED_ShowNum(1,7,KeyNum[2],2);
		OLED_ShowNum(1,10,KeyNum[3],2);
		
		OLED_Showdecimal(2,3,edgeCaptureInterval);

		OLED_ShowString(3,1,"y:");
		OLED_Showdecimal(3,3,fAngle[2]);
		
		OLED_ShowNum(4,1,capturedValue,6);

//		OLED_ShowString(2,1,"Kp:");
//		OLED_Showdecimal(2,4,pid_now.Kp);
//		OLED_ShowString(3,1,"Ki:");
//		OLED_Showdecimal(3,4,pid_now.Ki);
//		OLED_ShowString(4,1,"Kd:");
//		OLED_Showdecimal(4,4,pid_now.Kd);
		
		delay_ms(100);
    }
}


void System_Init(void)
{
	SYSCFG_DL_init();
	
	NVIC_EnableIRQ(GPIOA_INT_IRQn);				//开启按键引脚的GPIOA端口中断
	NVIC_EnableIRQ(GPIOB_INT_IRQn);				//开启按键引脚的GPIOB端口中断
	
//    NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);	//清除串口0中断标志
//    NVIC_EnableIRQ(UART_0_INST_INT_IRQN); 		//使能串口0中断
	
	NVIC_ClearPendingIRQ(UART_1_INST_INT_IRQN);	//清除串口1中断标志
    NVIC_EnableIRQ(UART_1_INST_INT_IRQN); 		//使能串口1中断
	
	NVIC_ClearPendingIRQ(UART_2_INST_INT_IRQN);	//清除串口2中断标志
    NVIC_EnableIRQ(UART_2_INST_INT_IRQN); 		//使能串口2中断
	
	NVIC_ClearPendingIRQ(UART_3_INST_INT_IRQN);	//清除串口3中断标志
    NVIC_EnableIRQ(UART_3_INST_INT_IRQN); 		//使能串口3中断
	 
    NVIC_ClearPendingIRQ(TIMER_0_INST_INT_IRQN);//清除定时器中断标志
    NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);		//使能定时器中断
	
//	NVIC_EnableIRQ(CAPTURE_0_INST_INT_IRQN);    // 使能捕获中断
//	DL_TimerG_startCounter(CAPTURE_0_INST);

//	NVIC_DisableIRQ(CAPTURE_0_INST_INT_IRQN);
//	DL_TimerG_stopCounter(CAPTURE_0_INST);
	
	SysTick_Init(); // 初始化SysTick定时器
	
	OLED_Init();
	
	OLED_ShowChar(1,15,'i');
	
//	delay_ms(500);
	
 	JP61P_Init();
}
